<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">重置密码</el-button>
        <el-button @click="resetForm('ruleForm')">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {resetPassword} from "../api/index";
import mixin from "../mixins";

export default {
  name: "Reset",
  data() {
    return {
      ruleForm: {
        id: localStorage.getItem('userId'),
        password: ''
      },
      rules: {
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.reset()
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    reset() {
      let _this = this;
      let token = _this.$store.getters.token
      let config = {
        headers:
          {
            'Authorization': token
          }
      }
      resetPassword(this.ruleForm, config).then(res => {
        if (res.data.code === 200) {
          mixin.methods.notify_success('重置密码成功')
          setTimeout(function () {
            _this.$router.back()
          }, 2000)
        } else {
          mixin.methods.notify_error('重置密码失败')
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
<style scoped>
.demo-ruleForm {
  width: 40%;
  height: 100%;
  padding-top: 100px;
  padding-bottom: 100px;
  margin-left: 10px;
  padding-left: 10px;
}
</style>
